package com.takeshequ.friends;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.takeshequ.Constants;
import com.takeshequ.R;
import com.takeshequ.homepage.HomePageActivity;
import com.takeshequ.main.BasicFragment;

public class FriendsFragment extends BasicFragment {
	private ListView listView;
	private static Context context;
	private List<ListItems> mListItems = new ArrayList<ListItems>();
	private static LayoutInflater mInflater;
	private PartAdapter adapter;
	private EditText searchInputView;
	private ImageView searchButton;
	private static final String ADD_FREIND = "addFriend";
	private static final String ADD_FREIND_FROM_ADDRESS_BOOK = "addFriendFromAddressBook";
	private Map<String, List<Friend>> friends = new HashMap<String, List<Friend>>();

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	public static FriendsFragment newInstance(Context con, ViewPager mp) {
		context = con;
		mInflater = LayoutInflater.from(context);
		return new FriendsFragment();

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseData.init();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.friends, container, false);
		listView = (ListView) view.findViewById(R.id.friends_listview);
		searchInputView = (EditText) view
				.findViewById(R.id.friends_search_edittext);
		searchButton = (ImageView) view.findViewById(R.id.friends_search_btn);

		mListItems = new ArrayList<ListItems>();
		adapter = new PartAdapter(inflater);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				if (view.getTag() instanceof String) {
					if (ADD_FREIND_FROM_ADDRESS_BOOK.equals(view.getTag())) {
						startActivity(new Intent(context,
								AddressBookContactsActivity.class));
					} else if (ADD_FREIND.equals(view.getTag())) {
						startActivity(new Intent(context,
								QueryFriendActivity.class));
					}
				} else {
					Friend friend = ((Friend) view.getTag());
					Constants.firend = friend;
					Constants.selectMemberId = friend.getMemberId();
					Intent intent = new Intent(context, HomePageActivity.class);
					startActivity(intent);
				}
			}
		});
		searchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String searchValue = searchInputView.getText().toString();
				convertToListItems(searchValue);
				adapter.notifyDataSetChanged();
			}
		});
		return view;

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	class PartAdapter extends BaseAdapter {
		private LayoutInflater inflater;

		public PartAdapter(LayoutInflater inflater) {
			this.inflater = inflater;
		}

		@Override
		public int getCount() {
			return mListItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mListItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public boolean isEnabled(int position) {
			return mListItems.get(position).isClickable();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (mListItems.size() > position) {
				return mListItems.get(position).getView(context, convertView,
						mInflater);
			}
			return null;
		}
	}  

	private void convertToListItems(String filterValue) {
		mListItems.clear();
		ContentItem2 contentItem1 = new ContentItem2();
		contentItem1.setTag(ADD_FREIND);
		contentItem1.setResId(R.drawable.friends_add_friend);
		contentItem1.setText("添加好友");
		mListItems.add(contentItem1);

		ContentItem2 contentItem2 = new ContentItem2();
		contentItem2.setTag(ADD_FREIND_FROM_ADDRESS_BOOK);
		contentItem2.setResId(R.drawable.friends_add_from_address_book);
		contentItem2.setText("从手机通讯录添加");
		mListItems.add(contentItem2);


		Iterator it = friends.entrySet().iterator();
		boolean hasChild = false;
		while (it.hasNext()) {
			Entry<String, List<Friend>> entry = (Entry) it.next();
			LabelItem label = new LabelItem(entry.getKey().toUpperCase());
			mListItems.add(label);
			for (Friend friend : entry.getValue()) {
				if (StringUtils.isEmpty(filterValue)
						|| friend.getNickName().contains(filterValue)) {
					ContentItem contentCarKeeper = new ContentItem(friend);
					hasChild = true;
					mListItems.add(contentCarKeeper);
				}
			}
			if (!hasChild) {
				mListItems.remove(label);
			}
			hasChild = false;
		}
	}

	@Override
	public void show() {
		friends = BaseData.getFriends();
		convertToListItems("");
		adapter.notifyDataSetChanged();
	}
}
